var showOnlyMissing = false;
function flipShowOnlyMissing() {
	showOnlyMissing = !showOnlyMissing;
	var tbl = document.getElementById('translationtable');
	// Traverse through the table rows (skipping the first header row)
	for (var i = 1; i < tbl.rows.length; i++) {
		if (tbl.rows[i].cells.length > 2 && tbl.rows[i].cells[2].children != null) {
			// Get the input field, which is in the third cell
			var input = tbl.rows[i].cells[2].children[0];
			
			if (input.value != '' && !showOnlyMissing) {
				// Show this row (again)
				tbl.rows[i].style.display = 'table-row';
			} else if (input.value != '') {
				// Hide this row
				tbl.rows[i].style.display = 'none';
			}
		}
	}
	// Change the button text
	var but = document.getElementById('showmissing');
	but.value = showOnlyMissing? 'Show all rows': 'Show only missing translations';
}

function storeArrayStringAdapter(input, separator) {
	alert(separator);

	var inputId = input.id;
	var inputIdParts = inputId.split("_");
	var inputIdNumber = inputIdParts[inputIdParts.length - 1];
	var baseKey = inputId.replace("_" + inputIdNumber, "");
	
	var hiddenInput = document.getElementById(baseKey);
	
	var eles = [];
	var inputs = document.getElementsByTagName("input");
	for(var i = 0; i < inputs.length; i++) {
	    if(inputs[i].id.indexOf(baseKey + "_") == 0) {
        	eles.push(inputs[i].id);
    	}
	}
	eles.sort();
	
	var newHiddenValue = '';
	for (var i=0; i<eles.length; i++) {
		if (i != 0) {
			newHiddenValue += separator;
		}
		var inputElement = document.getElementById(eles[i]);
		newHiddenValue += inputElement.value;
	}
	
	hiddenInput.value = newHiddenValue;
}

function emailCheck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	var dotSplits = str.split(dot);
	
	if (str.indexOf(at)==-1){
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false;
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		return false;
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false;
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		return false;
	 }
	
	 if (str.indexOf(" ")!=-1){
		return false;
	 }
	 
	 if (dotSplits[dotSplits.length-1].length < 2) {
		return false;
	 }

	 return true;
}

function validateSubmitInput() {
	var nameInput = document.getElementById('pastt_translator_name');
	var emailInput = document.getElementById('pastt_translator_email');
	
	var name = nameInput.value;
	var email = emailInput.value;
	
	var isValidationError = false;
	var errorName = "";
	var errorEmail = "";
	
	if (name != null) {
		name = name.replace(" ", "");
	}
	if (email != null) {
		email = email.replace(" ", "");
	}
	
	if (name == null || name == "") {
		errorName = "Your name is required to be able to submit changes";
		isValidationError = true;
	}
	
	if (email == null || email == "") {
		errorEmail = "Your E-mail is required to be able to submit changes";
		isValidationError = true;
	} else if (!emailCheck(email)) {
		errorEmail = "You should provide a valid e-mail to be able to submit changes";
		isValidationError = true;
	}
	
	if (isValidationError) {
		var error = "A validation error occured. Please fix this error before continuing:\n";
		if (errorName != "") {
			error = error + "\n* " + errorName;
		}
		if (errorEmail != "") {
			error = error + "\n* " + errorEmail;
		}
		alert(error);
		return false;
	}
	
	return true;
}